package cc.chenwenxi.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cc.chenwenxi.entity.Chat;
import cc.chenwenxi.entity.Chuxi;
import cc.chenwenxi.entity.LoveInfo;
import cc.chenwenxi.entity.MainInfo;
import cc.chenwenxi.entity.Slide;
import cc.chenwenxi.entity.Zan;
import cc.chenwenxi.repo.ChatRepository;
import cc.chenwenxi.repo.ChuxiRepository;
import cc.chenwenxi.repo.ZanRepository;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/")
public class InfoController {
	@Autowired
	ZanRepository zanRepo;
	@Autowired
	ChatRepository chatRepo;
	@Autowired
	ChuxiRepository chuxiRepo;

	private static MainInfo main = new MainInfo();
	private static List<Slide> slides = new ArrayList<>();
	static {
		main.setApp_name("希希and萍萍");
		main.setHe("陈文希");
		main.setShe("林萍");
		main.setHe_tel("13415000094");
		main.setShe_tel("18218926261");
		main.setLat("23.536690");
		main.setLng("116.798680");
		main.setDate("2019-01-23 星期三");
		main.setLunar("腊月十八");
		main.setHotel("澄海区溢城");
		main.setAddress("澄海区溪南镇莲南工业区金山路与福翔路交叉口");
		main.setShare("诚意邀请您参加我们的婚礼");
		main.setCover(
				"http://img.redocn.com/sheji/20150417/zanmenjiehunbakatonghaibaosheji_4181146.jpg");
		main.setQrimg("http://src.chenwenxi.cc/xcx1.jpg");// 小程序二维码
		main.setThumb("http://src.chenwenxi.cc/2.png");// 不知道是什么?分享?
		main.setCode("happy");

		// 图片集
//		slide.setId("83");
//		slide.setUserid("454");
		Slide slide = new Slide();
		slide.setImage("http://img.redocn.com/sheji/20150417/zanmenjiehunbakatonghaibaosheji_4181146.jpg");
		slide.setAddtime("1546046160");
		slides.add(slide);
		
		slide = new Slide();
		slide.setImage("http://src.chenwenxi.cc/timg.jpg");
		slide.setAddtime("1546046160");
//		slides.add(slide);
	}
	public static List<Zan> zans;
	public static List<Chat> chats;

	@RequestMapping("/list")
	public LoveInfo list(@RequestParam Map<String, Object> params) {
		LoveInfo loveInfo = new LoveInfo();
		loveInfo.setMainInfo(main);
		loveInfo.setSlideList(slides);
		loveInfo.setMusic_url("https://files.catbox.moe/o5kj0s.m4a");

		String type = params.get("c").toString();
		log.debug("类型:{}", type);
		if ("zan".equals(type)) {

			String appid = params.get("appid").toString();
			String nickname = params.get("nickname").toString();
			String face = params.get("face").toString();
			log.info("点赞:{},昵称:{},face:{}", appid, nickname, face);
			if (zanRepo.findOne(Example.of(new Zan().setNickname(nickname))).isPresent()) {
				loveInfo.setSuccess(false);
				loveInfo.setMsg("已经送过祝福了!!谢谢...");
			} else {
				Zan zan = new Zan(appid, face, nickname);
				Zan save = zanRepo.save(zan);
				if (save != null) {
					loveInfo.setSuccess(true);
					loveInfo.setMsg("谢谢祝福!!");
					zans = null;
				} else {
					loveInfo.setSuccess(false);
					loveInfo.setMsg("服务器资源有限,估计挂了");
				}
			}

		} else if ("send".equals(type)) {
			String appid = params.get("appid").toString();
			String nickname = params.get("nickname").toString();
			String face = params.get("face").toString();
			String words = params.get("words").toString();
			Chat chat = new Chat(appid, nickname, face, words);

			Chat save = chatRepo.save(chat);
			if (save != null) {
				loveInfo.setSuccess(true);
				loveInfo.setMsg("谢谢祝福!!");
				chats = null;
			} else {
				loveInfo.setSuccess(false);
				loveInfo.setMsg("服务器开小差,也有可能估计挂了");
			}
		} else if ("sign".equals(type)) {
			String appid = params.get("appid").toString();
			String nickname = params.get("nickname").toString();
			String face = params.get("face").toString();

			String name = params.get("name").toString();
			String tel = params.get("tel").toString();
			// 几个人出席
			String plan = params.get("plan").toString();
			// 备注
			String extra = params.get("extra").toString();

			Chuxi chuxi = new Chuxi(appid, face, nickname);
			chuxi.setName(name);
			chuxi.setTel(tel);
			chuxi.setPlan(plan);
			chuxi.setExtra(extra);

			Chuxi save = chuxiRepo.save(chuxi);
			if (save != null) {
				loveInfo.setSuccess(true);
				loveInfo.setMsg("!!恭候您的到来!");
			} else {
				loveInfo.setSuccess(false);
				loveInfo.setMsg("服务器开小差,也有可能估计挂了");
			}
		}

		// 查询赞
		if (zans == null) {
			List<Order> orders = new ArrayList<Order>();
			orders.add(new Order(Direction.DESC, "createTime"));
			zans = zanRepo.findAll(Sort.by(orders));
		}
		loveInfo.setZanLog(zans);
		loveInfo.setZanNum(zans.size() + "");
		if (chats == null) {
			List<Order> orders = new ArrayList<Order>();
			orders.add(new Order(Direction.DESC, "createTime"));
			chats = chatRepo.findAll(Sort.by(orders));
		}
		loveInfo.setChatList(chats);
		loveInfo.setChatNum(chats.size() + "");

		return loveInfo;
	}
}
